var entityCollection = require('../../../Base/server/controllers/EntityCollection'),
	terminalController = require('../../../CBO/server/controllers/Terminal.server.controller'),
	stationTerminalController = require('../../../Initialize/server/controllers/StationTerminal.server.controller');

//通过工位ID查找
exports.findByWorkLocationID = function(workLocationID, completeFun){
	var entity = entityCollection.getEntity("StationWork");
	entity.find({WorkLocation: workLocationID}, null, [{path: 'WorkLocation'}, {path: 'Work'}], function(err, records){
		if(err){
			var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询作业与工位交叉表错误';
            newErr.message = '请检查查询参数！';
            completeFun(newErr);
		}
		else{
			completeFun(null, records);
		}
	});
};

//通过作业ID查找
exports.findByWorkID = function(workID, completeFun){
	var entity = entityCollection.getEntity("StationWork");
	entity.find({Work: workID}, [{path: 'WorkLocation'}, {path: 'Work'}], null, function(err, records){
		if(err){
			var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询作业与工位交叉表错误';
            newErr.message = '请检查查询参数！';
            completeFun(newErr);
		}
		else{
			completeFun(null, records);
		}
	});
};

//根据终端设备UniqueID查找工位对应的工位作业交叉集合
exports.getStationWorksByTerminalUniqueID = function(terminalUniqueID, completeFun){
	var self = this;
	terminalController.findByUniqueID(terminalUniqueID, function(err, terminalRecord){
		if(err){
			completeFun(err);
		}
		else{
			if(terminalRecord == null || terminalRecord == undefined){
				var newErr = new Error();
	            newErr.leval = 9;
	            newErr.title = '查询终端失败';
	            newErr.message = '没有找到对应的终端记录，请检查查询参数！';
	            completeFun(newErr);
			}
			else{
				stationTerminalController.findByTerminalID(terminalRecord._id, function(err, stationTerminalRecord){
					if(err){
						completeFun(err);
					}
					else{
						if(stationTerminalRecord == null || stationTerminalRecord == undefined){
							var newErr = new Error();
				            newErr.leval = 9;
				            newErr.title = '查询工位终端交叉表失败';
				            newErr.message = '没有找到对应的终端工位交叉记录，请检查查询参数！';
				            completeFun(newErr);
						}
						else{
							self.findByWorkLocationID(stationTerminalRecord.WorkLocation._id, function(err, stationWorkRecords){
								if(err){
									completeFun(err);
								}
								else{
									completeFun(null, stationWorkRecords);
								}
							});
						}
					}
				});
			}
		}
	});
};

//根据终端设备ID查找工位对应的工位作业交叉集合
exports.getStationWorksByTerminalID = function(terminalID, completeFun){
	var self = this;
	stationTerminalController.findByTerminalID(terminalID, function(err, stationTerminalRecord){
		if(err){
			completeFun(err);
		}
		else{
			if(stationTerminalRecord == null || stationTerminalRecord == undefined){
				var newErr = new Error();
	            newErr.leval = 9;
	            newErr.title = '查询工位终端交叉表失败';
	            newErr.message = '没有找到对应的终端工位交叉记录，请检查查询参数！';
	            completeFun(newErr);
			}
			else{
				self.findByWorkLocationID(stationTerminalRecord.WorkLocation._id, function(err, stationWorkRecords){
					if(err){
						completeFun(err);
					}
					else{
						completeFun(null, stationWorkRecords);
					}
				});
			}
		}
	});
};
